﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using CXMLStudioIDE;
using CXMLStudioIDE.Common;

namespace CXMLStudioIDE.Common.Entities
{
    public class BusinessEntityCollection : CollectionBase
    {
        #region private members
        private int _ProjectID;
        #endregion

        #region public properties
        public int ProjectID
        {
            get { return _ProjectID; }
            set { _ProjectID = value; }
        }
        #endregion 

        #region Constructors
        public BusinessEntityCollection(int i_ProjectID)
        {
            this._ProjectID = i_ProjectID;
        }
        public BusinessEntityCollection()
        { 
        }
        #endregion

        #region Methods

        public BusinessEntity this[int index]
        {
            get
            {
                return ((BusinessEntity)List[index]);
            }
            set
            {
                List[index] = value;
            }
        }

        public int Add(BusinessEntity value)
        {
            return (List.Add(value));
        }

        public int IndexOf(BusinessEntity value)
        {
            return (List.IndexOf(value));
        }

        public void Insert(int index, BusinessEntity value)
        {
            List.Insert(index, value);
        }

        public void Remove(BusinessEntity value)
        {
            List.Remove(value);
        }

        public bool Contains(BusinessEntity value)
        {
            // If value is not of type Int16, this will return false.
            return (List.Contains(value));
        }

        public bool Contains(int context, string version, string entity_name)
        {
            foreach (BusinessEntity b in this)
            {
                if (b.EntityType.EntityTypeContextID == context && b.EntityVersion.FullVersion.Equals(version) && b.Name.Equals(entity_name))
                {
                    return true;
                }
            }
            return false;
        }

        public int IndexOf(int BusinessEntityID)
        {
            int result = -1;
            for (int i = 0; i < List.Count; i++)
            {
                BusinessEntity s = List[i] as BusinessEntity;
                if (s.ID == BusinessEntityID)
                    result = i;
            }
            return result;
        }

        public BusinessEntity GetBusinessEntityByTreeViewID(int TreeViewID)
        {
            foreach (BusinessEntity s in List)
            {
                if (s.TreeViewId == TreeViewID)
                    return s;
            }
            return null;        
        }

        public void Sort()
        {
            IComparer NameSorter = new SortByNameClass();
            InnerList.Sort(NameSorter);
        }

        #endregion
    }
    public class SortByNameClass : IComparer
    {
        public int Compare(object obj1, object obj2)
        {
            BusinessEntity b1 = (BusinessEntity)obj1;
            BusinessEntity b2 = (BusinessEntity)obj2;

            return (String.Compare(b1.Name, b2.Name));
        }
    }
}

